package com.gym.code.generator.impl.dao.sql.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.gym.code.generator.impl.dao.sql.SqlGenerator;
import com.gym.code.generator.model.mapping.field.FieldGenerateModel;
import com.gym.code.generator.model.mapping.field.ParameterFieldGenerateModel;
import com.gym.code.generator.model.mapping.method.MethodInsertModel;
import com.gym.code.generator.utils.LocalStringUtils;

/**
 * 
 * @author xinchun.wang 
   @email: 532002108@qq.com
 * @createTime 2015-4-15 下午8:21:45
 */
public class InsertSqlGenerator implements SqlGenerator<MethodInsertModel> {

	
	private static InsertSqlGenerator instance = new InsertSqlGenerator();
	public static InsertSqlGenerator getInstance(){
		return instance;
	}
	private InsertSqlGenerator(){}
	
	
	@Override
	public String generate(MethodInsertModel methodModel) {
		
		StringBuilder sqlBuilder = new StringBuilder();
		sqlBuilder.append("insert  into ").append(methodModel.getBeanGenerateModel().getTableName());
		sqlBuilder.append(" ( ");
		sqlBuilder.append(generateInsertColumn(methodModel.getParameterList()));
		sqlBuilder.append(") values (");
		sqlBuilder.append(StringUtils.repeat("?", ",", methodModel.getParameterList().size()));
		sqlBuilder.append(")");
		return sqlBuilder.toString();
	}

	
	private String generateInsertColumn(List<ParameterFieldGenerateModel> parameterList) {
		StringBuilder result = new StringBuilder();
		for(FieldGenerateModel item : parameterList){
			result.append(item.getColumnName()).append(",");
		}
		return LocalStringUtils.removeLastChar(result.toString(),',');
	}
	
}



